package com.dswz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dswz.dao.GoodsDao;
import com.dswz.dao.UserCartDao;
import com.dswz.po.Goods;
import com.dswz.po.GoodsQuery;
import com.dswz.po.UserCart;
import com.dswz.service.GoodsService;
import com.dswz.service.UserCartService;
import com.dswz.utils.DateUtil;
import com.dswz.utils.PageBean;

@Service("goodsService")
@Transactional
public class GoodsServiceImpl implements GoodsService {
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private UserCartService userCartService;
	@Autowired
	private UserCartDao userCartDao;

	@Transactional(readOnly = true)
	public PageBean getGoodsList(String username, Byte goodsType, String goodsTitle, String goodsCreateTime, Byte isDel,
			Integer page, Integer size) throws Exception {
		int count = 0;
		List<Goods> goodsList = new ArrayList();
		GoodsQuery goodsQuery = new GoodsQuery();

		goodsQuery.setPageNo(page);

		goodsQuery.setPageSize(size);

		goodsQuery.setOrderByClause("goods_order_no desc,goods_create_time desc");
		if ("admin".equals(username)) {
			GoodsQuery.Criteria createCriteria = goodsQuery.createCriteria();

			if (goodsType != null) {
				createCriteria.andGoodsTypeEqualTo(goodsType);
			}
			if (StringUtils.isNotEmpty(goodsTitle)) {
				createCriteria.andGoodsTitleLike("%" + goodsTitle + "%");
			}
			if (StringUtils.isNotEmpty(goodsCreateTime)) {
				String[] split = goodsCreateTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andGoodsCreateTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andGoodsCreateTimeGreaterThanOrEqualTo(startP);
				}
			}

			if (isDel != null) {
				createCriteria.andGoodsIsDelEqualTo(isDel);
			}
			count = this.goodsDao.countByExample(goodsQuery);
			goodsList = this.goodsDao.selectByExample(goodsQuery);
		} else {
			GoodsQuery.Criteria createCriteria = goodsQuery.createCriteria();

			if (goodsType != null) {
				createCriteria.andGoodsTypeEqualTo(goodsType);
			}
			if (StringUtils.isNotEmpty(goodsCreateTime)) {
				String[] split = goodsCreateTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andGoodsCreateTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andGoodsCreateTimeGreaterThanOrEqualTo(startP);
				}
			}

			if (isDel != null) {
				createCriteria.andGoodsIsDelEqualTo(isDel);
			}
			count = this.goodsDao.countByExample(goodsQuery);
			goodsList = this.goodsDao.selectByExample(goodsQuery);
		}

		PageBean pb = new PageBean(size.intValue(), count, page.intValue() + 1);

		pb.setContentList(goodsList);
		return pb;
	}

	public void insertGoods(Goods goods) throws Exception {
		goods.setGoodsId(UUID.randomUUID().toString().replaceAll("-", ""));

		if (goods.getGoodsOrderNo() == null) {
			goods.setGoodsOrderNo(Integer.valueOf(0));
		}
		goods.setGoodsIsDel(Byte.valueOf((byte) 0));
		goods.setGoodsCreateTime(new Date());
		this.goodsDao.insertSelective(goods);
	}

	public void deleteGoodss(List<String> goodsIds) throws Exception {
		Goods goods = new Goods();
		goods.setGoodsIsDel(Byte.valueOf((byte) 1));
		if ((goodsIds != null) && (goodsIds.size() > 0)) {
			for (int i = 0; i < goodsIds.size(); i++) {
				goods.setGoodsId(goodsIds.get(i));
				this.goodsDao.updateByPrimaryKeySelective(goods);
			}
		}
	}

	public void deleteGoods(String goodsId) throws Exception {
		Goods goods = new Goods();
		goods.setGoodsIsDel(Byte.valueOf((byte) 1));
		goods.setGoodsId(goodsId);
		this.goodsDao.updateByPrimaryKeySelective(goods);
	}

	public void updateGoods(Goods goods) throws Exception {
		this.goodsDao.updateByPrimaryKeySelective(goods);
		List<UserCart> userCartList = this.userCartService.getUserCartList(goods.getGoodsId(), Byte.valueOf((byte) 0));
		for (UserCart userCart : userCartList) {
			userCart.setGoodsName(goods.getGoodsTitle());
			userCart.setGoodsPic(goods.getGoodsPic());
			userCart.setGoodsPrice(goods.getGoodsPrice());
			this.userCartDao.updateByPrimaryKeySelective(userCart);
		}
	}

	@Transactional(readOnly = true)
	 public Goods getGoodsById(String id) throws Exception {
		return this.goodsDao.selectByPrimaryKey(id);
	}
}